package org.xtremeobjects.forms.ui.editors;

import javax.naming.OperationNotSupportedException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.part.EditorPart;
import org.xtremeobjects.domain.core.di.Injector;
import org.xtremeobjects.forms.ui.XCollectionEditorInput;
import org.xtremeobjects.forms.ui.XObjectEditorInput;

public class CollectionEditor extends EditorPart {

	// public static final String ID =
	// "org.xtremeobjects.forms.ui.editors.collectioneditor";
	public static final String ID = "org.xtremeobjects.workbech.rcp.collectioneditor";

	// TODO Needs to be whatever is mentioned in plugin.xml
	// @jve:decl-index=0:

	private CollectionEditorModel model = null;

	private CollectionEditorModelFactory factory = (CollectionEditorModelFactory) Injector
			.getInstance(CollectionEditorModelFactory.class);

	private CollectionEditorModel createModel(String className) {
		CollectionEditorModel m = null;
		try {
			m = factory.create(Class.forName(className));
		} catch (Throwable e) {
			e.printStackTrace();
		}

		return m;
	}

	private CollectionTable table = null;
	private ScrolledForm form;
	private FormToolkit toolkit;
	private Composite top;

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);

		model = createModel(getCollectionEditorInput().getClassName());

	}

	private XCollectionEditorInput getCollectionEditorInput() {
		return ((XCollectionEditorInput) this.getEditorInput());
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {

		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		top = form.getBody();
		TableWrapLayout layout = new TableWrapLayout();
		top.setLayout(layout);

		table = new CollectionTable(model.getClazzModel(), model
				.getCollection(), top, getEditorSite().getWorkbenchWindow());
		createButtonBar();
	}

	/***************************************************************************
	 * private void updateInput() { List collection = model.getCollection();
	 * exampleData = new WritableList(realm, collection,
	 * model.getImplementationClass()); top.setInput(exampleData); }/
	 **************************************************************************/

	private void createButtonBar() {

		Composite buttomBar = new Composite(top, SWT.NONE);
		buttomBar.setLayout(new RowLayout(SWT.HORIZONTAL));

		createNewButton(buttomBar);
		createRemoveButton(buttomBar);
		createUpdateButton(buttomBar);
	}

	private void createUpdateButton(Composite buttomBar) {
		final Button button2 = new Button(buttomBar, SWT.PUSH);
		button2.setText("Update");
		button2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				table.updateInput();
			}
		});
	}

	private void createRemoveButton(Composite buttomBar) {
		if (model.canRemove()) {
			final Button button1 = new Button(buttomBar, SWT.PUSH);
			button1.setText("Remove");
			/*******************************************************************
			 * button1.addSelectionListener(new SelectionAdapter() { public void
			 * widgetSelected(SelectionEvent event) { TableItem[] items =
			 * table.getSelection(); List objs = new ArrayList(); for (TableItem
			 * item : items) { if (item.getData() != null)
			 * objs.add(item.getData()); } if (objs.size() > 0) { try {
			 * model.remove(objs); updateInput(); } catch
			 * (OperationNotSupportedException ex) { ex.printStackTrace(); } } }
			 * }); /
			 ******************************************************************/
		}
	}

	private void createNewButton(Composite buttomBar) {
		if (model.canCreateNew()) {
			final Button button = new Button(buttomBar, SWT.PUSH);
			button.setText("Add new ...");
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					try {
						model.createNew();
						table.updateInput();
					} catch (OperationNotSupportedException ex) {
						ex.printStackTrace();
					}
				}
			});
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

} // @jve:decl-index=0:visual-constraint="10,10,455,201"

